
        <html>
			<head>
			     <title>Burpsuite中protobuf数据流的解析 - Vincent</title>
				 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
			</head>
			<body>
				<h1>原文地址:<a href="http://drops.wooyun.org/tools/12680">http://drops.wooyun.org/tools/12680</a></h1>
				
      <p>
        <p><strong>Author:<a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="94722635732a29d47d0c2b7d131873332f711e3c713a1d71113c">[email&#160;protected]</a><script data-cfhash='f9e31' type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script></strong></p>

<h1>0x00 前言</h1>

<hr />

<p>对于protobuf over-HTTP的数据交互方式Burpsuite不能正确的解析其中的数据结构，需要Burpsuite扩展才能解析，笔者使用mwielgoszewski的burp-protobuf-decoder<sup>【1】</sup>扩展实践了protobuf数据流的解析，供有需要的同学学习交流。笔者实践使用的环境： burpsuite+python2.7+protobuf2.5.0。</p>

<!--more-->

<h1>0x01 安装burp-protobuf-decoder扩展</h1>

<hr />

<p>burp-protobuf-decoder<sup>【1】</sup>扩展是基于protobuf库（2.5.x版本）开发的burpsuite python扩展，可用于解析、篡改 request/response中protobuf数据流。从<a href="https://github.com/mwielgoszewski/burp-protobuf-decoder">https://github.com/mwielgoszewski/burp-protobuf-decoder</a>下载该扩展源码，然后解压。</p>

<p>该扩展是基于protobuf和jython实现的。先下载protobuf 2.5.0<sup>【2】</sup>源码进行编译，编译方法请参考其README.txt文件。需求在burpsuite的Extender中配置Jython<sup>【3】</sup>的路径：</p>

<p><img src="http://static.wooyun.org//drops/20160203/201602030751069811211.jpg" alt="p1" /></p>

<p>Burpsuite中添加扩展：</p>

<ol>
<li><p>在Burpsuite的Extender窗口中点击“Add”按钮，弹出的“Load Burp Extension”窗口中选择如下信息：</p>

<p><img src="http://static.wooyun.org//drops/20160203/201602030751094116721.jpg" alt="p2" /></p></li>
<li><p>然后Next，当看到如下信息时表示扩展加载成功：</p>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075111668323.jpg" alt="p3" /></p></li>
</ol>

<p><strong>Tips：</strong></p>

<ol>
<li><p>加载扩展时提示<code>“Error calling protoc: Cannot run program "protoc" (in directory "******"): error=2, No such file or directory”</code>错误</p>

<p>解决办法：修改protoburp.py中调用protoc命令的路径，有多处，如：</p>

<p>将<code>process = subprocess.Popen(['protoc', '--version']</code>中<code>'protoc'</code>改为<code>'/home/name/protobuf/src/protoc'</code>。</p></li>
<li><p>加载扩展碰到<code>cannot import name symbol_database</code>错误</p>

<p>可能是你使用的protoc与扩展所使用protobuf python库版本不一致原因，一种解决办法是下载protobuf 2.5.0源码编译后，修改protoburp.py中对应的路径，再加载扩展。</p></li>
<li><p>扩展加载成功了，但不能解析protobuf数据流</p>

<p>该扩展通过判断头部“content-type”是否为“<code>'application/x-protobuf'</code>”来决定是否解析数据，你可以修改protoburp.py中的isEnabled()方法让其工作。</p></li>
</ol>

<h1>0x02 protobuf简介</h1>

<hr />

<p>protobuf是Google开源的一个跨平台的结构化数据存储格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。</p>

<p>protobuf通过定义“.proto”文件来描述数据的结构。.proto文件中用 “Message”来表示所需要序列化的数据的格式。Message由Field组成，Field类似Java或C++中成员变量，通常一个Field的定义包含修饰符、类型、名称和ID。下面看一个简单的.proto文件的例子：</p>

<pre><code>#!cpp
syntax = "proto2";
package tutorial;
message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}
message AddressBook {
  repeated Person person = 1;
}
</code></pre>

<p>使用下面的python代码生成二进制数据流：</p>

<pre><code>#!python
import addressbook_pb2
address_book = addressbook_pb2.AddressBook()
person = address_book.person.add()
person.id = 9
person.name = 'Vincent'
person.email = <a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="597e0f30373a3c372d192d3c2a2d773a3634">[email&#160;protected]</a><script data-cfhash='f9e31' type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script>'
phone = person.phone.add()
phone.number = '15011111111'
phone.type = 2
f = open('testAb', "wb")
f.write(address_book.SerializeToString())
f.close()
</code></pre>

<p>序列化后的二进制数据流如下：</p>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075114635374.jpg" alt="p4" /></p>

<p>有关Protobuf的语法网上已有很多文章了，你可以网上搜索或参考其官网<sup>【4】</sup>说明。</p>

<h3>2.1Varint编码</h3>

<p>Protobuf的二进制使用Varint编码。Varint 是一种紧凑的表示数字的方法。它用一个或多个字节来表示一个数字，值越小的数字使用越少的字节数。这能减少用来表示数字的字节数。</p>

<p>Varint 中的每个 byte 的最高位 bit 有特殊的含义，如果该位为 1，表示后续的 byte 也是该数字的一部分，如果该位为 0，则结束。其他的 7 个 bit 都用来表示数字。因此小于 128 的数字都可以用一个 byte 表示。大于 128 的数字，比如 300，会用两个字节来表示：1010 1100 0000 0010。</p>

<p>下图演示了protobuf如何解析两个 bytes。注意到最终计算前将两个 byte 的位置相互交换过一次，这是因为protobuf 字节序采用 little-endian 的方式。</p>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075115128765.jpg" alt="p5" /> <em>(图片来自网络)</em></p>

<h3>2.2数值类型</h3>

<p>Protobuf经序列化后以二进制数据流形式存储，这个数据流是一系列key-Value对。Key用来标识具体的Field，在解包的时候，Protobuf根据 Key 就可以知道相应的 Value 应该对应于消息中的哪一个 Field。</p>

<p>Key 的定义如下：</p>

<p><code>(field_number &lt;&lt; 3) | wire_type</code></p>

<p>Key由两部分组成。第一部分是 field&#95;number，比如消息 tutorial .Person中 field name 的 field&#95;number 为 1。第二部分为 wire_type。表示 Value 的传输类型。Wire Type 可能的类型如下表所示：</p>

<table>
<thead>
<tr>
  <th align="left">Type</th>
  <th align="left">Meaning</th>
  <th align="left">Used For</th>
</tr>
</thead>
<tbody>
<tr>
  <td align="left">０</td>
  <td align="left">Varint</td>
  <td align="left">int32, int64, uint32, uint64, sint32, sint64, bool, enum</td>
</tr>
<tr>
  <td align="left">1</td>
  <td align="left">64-bit</td>
  <td align="left">fixed64, sfixed64, double</td>
</tr>
<tr>
  <td align="left">2</td>
  <td align="left">Length-delimi</td>
  <td align="left">string, bytes, embedded messages, packed repeated fields</td>
</tr>
<tr>
  <td align="left">3</td>
  <td align="left">Start group</td>
  <td align="left">Groups (deprecated)</td>
</tr>
<tr>
  <td align="left">4</td>
  <td align="left">End group</td>
  <td align="left">Groups (deprecated)</td>
</tr>
<tr>
  <td align="left">5</td>
  <td align="left">32-bit</td>
  <td align="left">fixed32, sfixed32, float</td>
</tr>
</tbody>
</table>

<p>以数据流：08 96 01为例分析计算key-value的值：</p>

<pre><code>#!bash
08 = 0000 1000b
    =&gt; 000 1000b(去掉最高位)
    =&gt; field_num = 0001b(中间4位), type = 000(后3位)
    =&gt; field_num = 1, type = 0(即Varint)
96 01 = 1001 0110 0000 0001b
    =&gt; 001 0110 0000 0001b(去掉最高位)
    =&gt; 1 001 0110b(因为是little-endian)
    =&gt; 128+16+4+2=150
</code></pre>

<p>最后得到的结构化数据为：</p>

<p><code>1:150</code></p>

<p>其中1表示为<code>field_num</code>，150为value。</p>

<h3>2.3手动反序列化</h3>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075118511076.jpg" alt="p6" /></p>

<p>以上面例子中序列化后的二进制数据流进行反序列化分析：</p>

<pre><code>#!bash
0A = 0000 1010b =&gt; field_num=1, type=2;
2E = 0010 1110b =&gt; value=46;
0A = 0000 1010b =&gt; field_num=1, type=2;
07 = 0000 0111b =&gt; value=7;
</code></pre>

<p>读取7个字符“Vincent”；</p>

<pre><code>#!bash
10 = 0001 0000 =&gt; field_num=2, type=0;
09 = 0000 1001 =&gt; value=9;
1A = 0001 1010 =&gt; field_num=3, type=2;
10 = 0001 0000 =&gt; value=16;
</code></pre>

<p><a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="8e6621356b0118bfbe6a36246b23196922286c0e12d8e7e0edebe0facefaebfdfaa0ede1e36c0e13">[email&#160;protected]</a><script data-cfhash='f9e31' type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script>；</p>

<pre><code>#!bash
22 = 0010 0010 =&gt; field_num=4, type=2;
0F = 0000 1111 =&gt; value=15;
0A = 0000 1010 =&gt; field_num=1, type=2;
0B = 0000 1011 =&gt; value=11;
</code></pre>

<p>读取11个字符“15011111111”；</p>

<pre><code>#!bash
10 = 0001 0000 =&gt; field_num=2, type=0;
02 = 0000 0010 =&gt; value=2;
</code></pre>

<p>最后得到的结构化数据为：</p>

<pre><code>#!bash
1 {
  1: "Vincent"
  2: 9
  3: "<a class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="43152a2d20262d3703372630376d202c2e">[email&#160;protected]</a><script data-cfhash='f9e31' type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script>"
  4 {
    1: "15011111111"
    2: 2
  }
}
</code></pre>

<h3>2.4使用protoc反序列化</h3>

<p>实现操作经常碰到较复杂、较长的流数据，手动分析确实麻烦，好在protoc加“<code>decode_raw</code>”参数可以解流数据，我实现了一个python脚本供使用：</p>

<pre><code>#!python
def decode(data):
    process = subprocess.Popen(['/usr/local/bin/protoc', '--decode_raw'],
    stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

    output = error = None
    try:
        output, error = process.communicate(data)
    except OSError:
        pass
    finally:
        if process.poll() != 0:
            process.wait()
    return output

f = open(sys.argv[1], "rb")
data = f.read()
print 'data:\n',decode(data)
f.close()
</code></pre>

<p>使用<code>python decode.py &lt;proto.bin&gt;</code>即可反序列化，其中proto.bin为protobuf二进制数据流文件。得到结构化的数据后我们可以逐步分析，猜测每个Field的名称，辅助协议、数据结构等逆向分析。</p>

<h1>0x03 burpsuite+protobuf实战</h1>

<hr />

<p>用webpy模拟protobuf over-HTTP的web app。</p>

<p>服务端<code>overHttp_server.py</code>内容如下：</p>

<pre><code>#!python
#!/usr/bin/env python
#coding: utf8
#author: Vincent
import web
import time
import os

urls = (
    "/",  "default",
    )
app = web.application(urls, globals())

class default:
    def GET(self):
        return 'hello world.'
    def POST(self):
        reqdata = web.data()

        print 'client request:'+reqdata
        resdata = reqdata.split(':')[-1]
        web.header('Content-type', 'application/x-protobuf')
        return resdata
if __name__ == "__main__":
app.run()
</code></pre>

<p>客户端<code>overHttp_client.py</code>内容如下：</p>

<pre><code>#!python
#!/usr/bin/env python
#coding: utf8
#author: Vincent
import urllib
import urllib2
import json
import addressbook_pb2
import sys

proxy = 'http://&lt;ip&gt;:8888'
target = "http://&lt;ip&gt;:8080/"
enable_proxy = True  
proxy_handler = urllib2.ProxyHandler({"http" : proxy})  
null_proxy_handler = urllib2.ProxyHandler({})  
if enable_proxy:  
    opener = urllib2.build_opener(proxy_handler)  
else:  
    opener = urllib2.build_opener(null_proxy_handler)  
urllib2.install_opener(opener)

def doPostReq():
    url = target
    address_book = addressbook_pb2.AddressBook()
    f = open('testAb', "rb")
    address_book.ParseFromString(f.read())
    ad_serial = address_book.SerializeToString()
    f.close()
    data = ad_serial
    opener = urllib2.build_opener(proxy_handler, urllib2.HTTPCookieProcessor())
    req = urllib2.Request(url, data, headers={'Content-Type': 'application/x-protobuf'})
    response = opener.open(req)
    return response.read()

resp = doPostReq()
print 'response:',resp
</code></pre>

<h3>3.1proto文件逆向分析</h3>

<p>启动服务端：<code>python overHttp_server.py &lt;ip&gt;:8080</code></p>

<p>客户端请求：<code>python overHttp_client.py</code></p>

<p>此时burp中已解析出protobuf数据，如下图：</p>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075121375967.jpg" alt="p7" /></p>

<p>但是这个结构的可读性还是比较差，我们可以通过逆向分析逐步猜测字段名称、类型，然后再解析，方便实现协议的逆向、安全测试等。</p>

<p>对这个结构我们可以还原成以下proto文件：</p>

<pre><code>#!cpp
syntax = "proto2";
package reversed.proto1;

message Msg {
  optional string _name = 1;
  optional int32 field2 = 2;
  optional string _email = 3;

  message subMsg1 {
    required string _phone = 1;
    optional int32 sub1_field2 = 2;
  }

  repeated subMsg1 field4 = 4;
}

message Root {
  repeated Msg msg = 1;
}
</code></pre>

<p>然后使用右键的“Load .proto”加载该文件：</p>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075123484058.jpg" alt="p8" /></p>

<p>再看解析结果：</p>

<p><img src="http://static.wooyun.org//drops/20160203/20160203075125983819.jpg" alt="p9" /></p>

<h3>3.2数据篡改</h3>

<p>打开request拦截：</p>

<p><img src="http://static.wooyun.org//drops/20160203/201602030751289651710.jpg" alt="p10" /></p>

<p>运行<code>python overHttp_client.py</code>发送请求。拦截到request后，把<code>sub1_field2</code>改为999。</p>

<p><img src="http://static.wooyun.org//drops/20160203/2016020307512933236111.jpg" alt="p11" /></p>

<p>“Forward”后看request数据，已被篡改：</p>

<p><img src="http://static.wooyun.org//drops/20160203/201602030751317063912.jpg" alt="p12" /></p>

<h1>0x04 参考</h1>

<hr />

<ul>
<li>【1】<a href="https://github.com/mwielgoszewski/burp-protobuf-decoder">https://github.com/mwielgoszewski/burp-protobuf-decoder</a></li>
<li>【2】<a href="https://github.com/google/protobuf/tree/v2.5.0">https://github.com/google/protobuf/tree/v2.5.0</a></li>
<li>【3】<a href="https://wiki.python.org/jython/InstallationInstructions">https://wiki.python.org/jython/InstallationInstructions</a></li>
<li>【4】<a href="https://developers.google.com/protocol-buffers/docs/proto">https://developers.google.com/protocol-buffers/docs/proto</a></li>
<li>【5】<a href="https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/">https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/</a></li>
<li>【6】<a href="https://developers.google.com/protocol-buffers/docs/overview">https://developers.google.com/protocol-buffers/docs/overview</a></li>
<li>【7】<a href="http://www.tssci-security.com/archives/2013/05/30/decoding-and-tampering-protobuf-serialized-messages-in-burp/">http://www.tssci-security.com/archives/2013/05/30/decoding-and-tampering-protobuf-serialized-messages-in-burp/</a></li>
</ul>      </p>
    
			</body>
		</html>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          